<?php
namespace app\admin\controller;
use think\Db;
use think\Validate;
use app\admin\model\Areas;
use app\admin\model\Networks;
class Area extends Common
{
	public function _initialize()
    {
        parent::_initialize();
        $this->Areas = new Areas;
    }
    public function index($page=null,$key=null){
        // try{
        //     $megaports = new \clt\Monthly('megaport');
        //                  $result = $megaports->BuyPort(['name'=>'222','term'=>1,'pop_id'=>284]);
        //              }catch(\Exception $e){
        //                 var_dump($e->getMessage());
        //              }
    	if($this->request->isPost()){
    		$page = (int)$page > 0 ? $page : 1;
            $map=[];
            $map['pid'] = 0;
            if(!empty($key)){
            	$map['name'] = array('like',"%".$key."%");
            }
            $lists = $this->Areas
            	->where($map)
                ->order('name asc,sort asc,id asc')
                ->paginate(array('list_rows'=>$this->pageSize,'page'=>$page))
                ->toArray();
            return $result = ['code'=>0,'msg'=>lang('No Data...'),'data'=>$lists['data'],'count'=>$lists['total'],'rel'=>1];
    	}
    	$this->assign('title',lang('District Management'));
    	return view();
    }
    public function create($id=null){
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		if(empty($data['name'])){
    			return ['code'=>1,'msg'=>lang('Please input area name')];
    		}
    		$data['pid'] = 0;
    		if($id > 0 ){
    			$result = $this->Areas->update($data);
    		}else{
    			$data['create_time'] = time();
    			$data['status'] = 1;
    			$result = $this->Areas->insert($data);
    		}
    		if($result){
    			return ['code'=>1,'msg'=>lang('The data is saved successfully!')];
    		}
    		return ['code'=>0,'msg'=>lang('Data save failed!')];
    	}
    	$row = [];
    	if(intval($id) > 0){
    		$row = $this->Areas->get($id);
    	}
        $this->assign('title',lang('编辑地区'));
    	$countrys = Areas::getAreas(0);
    	$this->assign('countrys',$countrys);
    	$this->assign('row',$row);
    	return view();
    }
    /*删除数据*/
    public function delete($id=null){
    	if($id > 0 && $this->Areas->where(['id'=>$id])->delete()){
    		$this->Areas->where(['pid'=>$id])->delete();
    		return ['code'=>1,'msg'=>lang('Delete success!')];
    	}
    	return ['code'=>0,'msg'=>lang('Delete failure!')];
    }
    /*城市数据*/
    public function citys($country_id=null,$page=null,$key=null){
    	if($this->request->isPost()){
    		$page = (int)$page > 0 ? $page : 1;
            $map=[];
            $map['pid'] = $country_id;
            if(!empty($key)){
            	$map['name'] = array('like',"%".$key."%");
            }
            $lists = $this->Areas
            	->where($map)
                ->order('name asc,sort asc,id asc')
                ->paginate(array('list_rows'=>$this->pageSize,'page'=>$page))
                ->toArray();
            return $result = ['code'=>0,'msg'=>lang('No Data...'),'data'=>$lists['data'],'count'=>$lists['total'],'rel'=>1];
    	}
    	$country = $this->Areas->get($country_id);
    	if(empty($country)){
    		$this->error(lang('Area does not exist'),U('index'));
    	}
    	$this->assign('country',$country);
    	$this->assign('title',$country['name']);
    	return view();
    }
    public function addcity($country_id=null,$id=null){
    	$country = $this->Areas->get($country_id);
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		if(empty($country)){
	    		return ['code'=>0,'msg'=>lang('Area does not exist')];
	    	}
    		if(empty($data['name'])){
    			return ['code'=>0,'msg'=>lang('Please choose the city')];
    		}
    		if(empty($data['location'])){
    			return ['code'=>0,'msg'=>lang('Please input coordinates')];
    		}
    		list($data['lng'],$data['lat']) = explode(",", $data['location']);
    		$data['pid'] = $country['id'];
    		if($id > 0 ){
    			$result = $this->Areas->update($data);
    		}else{
    			$data['create_time'] = time();
    			$data['status'] = 1;
    			$result = $this->Areas->insert($data);
    		}
    		if($result){
    			return ['code'=>1,'msg'=>lang('The data is saved successfully!'),'url'=>U('citys',array('country_id'=>$country['id']))];
    		}
    		return ['code'=>0,'msg'=>lang('Data save failed!')];
    	}
    	if(empty($country)){
    		$this->error(lang('Area does not exist'),U('index'));
    	}
    	$row = [];
    	$row['location'] = '';
    	if(intval($id) > 0){
    		$row = $this->Areas->get($id);
    		$row['location'] = '';
    		if(intval($row['lng']) != 0 && intval($row['lat']) != 0){
    			$row['location'] = $row['lng'].",".$row['lat'];
    		}
    		
    	}
    	$this->assign('row',$row);
    	$this->assign('country',$country);
    	$this->assign('title',$country['name']);
    	return view();
    }
    public function lists($pid=0){
        $lisst = Areas::getAreas($pid);
        if($lisst){
            return ['code'=>1,'msg'=>lang('Set up successfully.'),'items'=>$lisst];
        }else{
            return ['code'=>0,'msg'=>lang('Failed.')];
        }
    }
}